﻿using System;
using System.Collections.Generic;
using log4net;
using Spring.Data.NHibernate;
using uNhAddIns.Adapters;
using uNhAddIns.Adapters.CommonTests;
using uNhAddIns.Adapters.CommonTests.ConversationManagement;
using uNhAddIns.SessionEasier.Conversations;

namespace uNhAddIns.SpringAdapters.Tests.Implementations
{
    public class ConversationCreationInterceptor : IMyServiceConversationCreationInterceptor
    {
        public const string StartingMessage = "Starting";
        public const string StartedMessage = "Started";

        public ILog Log
        {
            get { return LogManager.GetLogger(typeof(ConversationCreationInterceptor)); }
        }

        #region Implementation of IConversationCreationInterceptor

        public void Configure(IConversation conversation)
        {
            conversation.Starting += ((x, y) => Log.Debug(StartingMessage));
            conversation.Started += ((x, y) => Log.Debug(StartedMessage));
        }

        #endregion
    }

    public class SillyDao : HibernateTemplate, ISillyDao
    {
        //private ISessionFactory factory;

        public SillyDao()
        {
            
        }

        //public SillyDao()
        //{
        //    //this.factory = factory;
        //}

        /// <summary>
        /// Required for testing - don't do this in production code
        ///// </summary>
        //public ISessionFactory Factory
        //{
        //    get { return factory; }
        //    set { factory = value; }
        //}

        public Silly Get(Guid id)
        {
            return SessionFactory.GetCurrentSession().Get<Silly>(id);
        }

        public IList<Silly> GetAll()
        {
            return SessionFactory.GetCurrentSession().CreateQuery("from Silly").List<Silly>();
        }

        public Silly MakePersistent(Silly entity)
        {
            SessionFactory.GetCurrentSession().SaveOrUpdate(entity);
            return entity;
        }

        public void MakeTransient(Silly entity)
        {
            SessionFactory.GetCurrentSession().Delete(entity);
        }
    }

    [PersistenceConversational]
    public class SillyCrudModel : ISillyCrudModel
    {
        //private readonly IDaoFactory factory;

        public SillyCrudModel()
        {

        }

        protected ISillyDao EntityDao { get; set; }

        #region Implementation of ISillyCrudModel
        [PersistenceConversation]
        public virtual IList<Silly> GetEntirelyList()
        {
            return EntityDao.GetAll();
        }

        [PersistenceConversation]
        public virtual Silly GetIfAvailable(Guid id)
        {
            return EntityDao.Get(id);
        }

        [PersistenceConversation]
        public virtual Silly Save(Silly entity)
        {
            return EntityDao.MakePersistent(entity);
        }

        [PersistenceConversation]
        public virtual void Delete(Silly entity)
        {
            EntityDao.MakeTransient(entity);
        }

        [PersistenceConversation(ConversationEndMode = EndMode.CommitAndContinue)]
        public virtual void ImmediateDelete(Silly entity)
        {
            EntityDao.MakeTransient(entity);
        }

        [PersistenceConversation(ConversationEndMode = EndMode.End)]
        public virtual void AcceptAll()
        {
            // method for use-case End
        }

        [PersistenceConversation(ConversationEndMode = EndMode.Abort)]
        public virtual void Abort()
        {
            // method for use-case Abort
        }

        #endregion
    }
}
